package com.shop.cloud.service.hongfu.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shop.cloud.common.mybatis.BaseService;
import com.shop.cloud.common.mybatis.MybatisDao;
import com.shop.cloud.dao.hongfu.InviteHistoryDao;
import com.shop.cloud.domain.hongfu.DistributionMsg;
import com.shop.cloud.domain.hongfu.InviteHistory;
import com.shop.cloud.domain.hongfu.User;
import com.shop.cloud.service.hongfu.IDistributionMsgService;
import com.shop.cloud.service.hongfu.IInviteHistoryService;
import com.shop.cloud.service.hongfu.IUserService;
import com.shop.cloud.util.Page;

/**
 * @author Yan Weiwei
 */
@Service("inviteHistoryService")
public class InviteHistoryService extends BaseService<InviteHistory> implements IInviteHistoryService{
    
    @Autowired
	private InviteHistoryDao inviteHistoryDao;
    
    @Autowired
    private IUserService userService;
    
    @Autowired
    private IDistributionMsgService distributionMsgService;

	@Override
	public MybatisDao<InviteHistory> getEntityDao() {
		return inviteHistoryDao;
	}

	/**
	 * 邀请注册时添加关联关系
	 */
	@Override
	public void handleInviteRegister(String inviteCode, String qrcode, String registerID) {
		
		if(StringUtils.isNotEmpty(inviteCode) || StringUtils.isNotEmpty(qrcode)){
			//扫描二维码或输入邀请码注册
			//先获取邀请者信息
			User dataUser = new User();
			dataUser.setInviteCode(inviteCode);
			dataUser.setQrcode(qrcode);
			List<User> toUser = userService.find(dataUser);
			if(null != toUser && toUser.size() > 0){
				//存在邀请者
				InviteHistory inviteHistory = new InviteHistory();
				inviteHistory.setUserId(toUser.get(0).getId());
				inviteHistory.setInviteUserId(new Long(registerID));
				inviteHistory.setInviteTime(new Timestamp(new Date().getTime()));
				//保存邀请记录
				this.insert(inviteHistory);
			}
		}
	}

	@Override
	public Page<InviteHistory> getPage(InviteHistory inviteHistory) {
		
		Page<InviteHistory> page = this.page(inviteHistory);
		
		List<InviteHistory> list = page.getData();
		if(null != list && list.size() > 0){
			for(InviteHistory invite : list){
				Long inviteUserId = invite.getInviteUserId();
				User user = userService.get(inviteUserId);
				if(null != user){
					invite.setInviteUserNickName(user.getNickname());
					invite.setInviteUserImg(user.getPortrait());
				}
			}
		}
		page.setData(list);
		
		return page;
	}

	@Override
	public InviteHistory handleInviteMsg(InviteHistory inviteHistory) {
		
		InviteHistory result = new InviteHistory();
		
		List<InviteHistory> list = this.find(inviteHistory);
		result.setInviteCount(list.size());
		
		DistributionMsg param = new DistributionMsg();
		param.setToUser(inviteHistory.getUserId());
		List<DistributionMsg> params = distributionMsgService.find(param);
		if(null != params && params.size() > 0){
			int invitePoints = 0;
			float inviteAmount = 0f;
			
			for(int i = 0; i < params.size(); i ++){
				DistributionMsg insData = params.get(i);
				int insType = insData.getType();
				if(insType == 0){
					invitePoints = invitePoints + (insData.getDistriNum().intValue());
				}else{
					inviteAmount = inviteAmount + insData.getDistriNum();
				}
			}
			result.setInvitePoints(invitePoints);
			result.setInviteAmount(inviteAmount);
		}
		
		return result;
	}
    
}